{% macro errorList(errors) %}
    {% include "_includes/forms/errorList" %}
{% endmacro %}


{# Inputs #}


{% macro hidden(config) -%}
    {% include "_includes/forms/hidden" with config only %}
{%- endmacro %}


{% macro text(config) %}
    {% include "_includes/forms/text" with config only %}
{% endmacro %}


{% macro password(config) %}
    {% include "_includes/forms/password" with config only %}
{% endmacro %}


{% macro copytext(config) %}
    {% include "_includes/forms/copytext" with config only %}
{% endmacro %}


{% macro date(config) %}
    {% include "_includes/forms/date" with config only %}
{% endmacro %}


{% macro time(config) %}
    {% include "_includes/forms/time" with config only %}
{% endmacro %}


{% macro color(config) %}
    {% include "_includes/forms/color" with config only %}
{% endmacro %}


{% macro textarea(config) %}
    {% include "_includes/forms/textarea" with config only %}
{% endmacro %}


{% macro select(config) %}
    {% include "_includes/forms/select" with config only %}
{% endmacro %}


{% macro multiselect(config) %}
    {% include "_includes/forms/multiselect" with config only %}
{% endmacro %}


{% macro checkbox(config) %}
    {% include "_includes/forms/checkbox" with config only %}
{% endmacro %}


{% macro checkboxGroup(config) %}
    {% include "_includes/forms/checkboxGroup" with config only %}
{% endmacro %}


{% macro checkboxSelect(config) %}
    {% include "_includes/forms/checkboxSelect" with config only %}
{% endmacro %}


{% macro radio(config) %}
    {% include "_includes/forms/radio" with config only %}
{% endmacro %}


{% macro radioGroup(config) %}
    {% include "_includes/forms/radioGroup" with config only %}
{% endmacro %}


{% macro file(config) %}
    {% include "_includes/forms/file" with config only %}
{% endmacro %}


{% macro lightswitch(config) %}
    {% include "_includes/forms/lightswitch" with config only %}
{% endmacro %}


{% macro editableTable(config) %}
    {% include "_includes/forms/editableTable" with config only %}
{% endmacro %}


{% macro elementSelect(config) %}
    {% include "_includes/forms/elementSelect" with config only %}
{% endmacro %}


{% macro autosuggest(config) %}
    {% include "_includes/forms/autosuggest" with config only %}
{% endmacro %}


{% macro fieldLayoutDesigner(config) %}
    {% include "_includes/forms/fieldLayoutDesigner" with config only %}
{% endmacro %}


{# Fields #}


{% macro field(config, input) %}
    {% include "_includes/forms/field" with config|merge({ input: input }) only %}
{% endmacro %}


{% macro textField(config) %}
    {% set config = config|merge({id: config.id ?? "text#{random()}"}) %}
    {% if config.unit is defined %}
        {% set input %}
            <div class="flex">
                <div class="textwrapper">{{ _self.text(config) }}</div>
                <div class="label light">{{ config.unit }}</div>
            </div>
        {% endset %}
    {% else %}
        {% set input = _self.text(config) %}
    {% endif %}
    {{ _self.field(config, input) }}
{% endmacro %}


{% macro copytextField(config) %}
    {% set config = config|merge({id: config.id ?? "copytext#{random()}"}) %}
    {{ _self.field(config, _self.copytext(config)) }}
{% endmacro %}


{% macro passwordField(config) %}
    {% set config = config|merge({id: config.id ?? "password#{random()}"}) %}
    {{ _self.field(config, _self.password(config)) }}
{% endmacro %}


{% macro dateField(config) %}
    {% set config = config|merge({id: config.id ?? "date#{random()}"}) %}
    {{ _self.field(config, _self.date(config)) }}
{% endmacro %}


{% macro timeField(config) %}
    {% set config = config|merge({id: config.id ?? "time#{random()}"}) %}
    {{ _self.field(config, _self.time(config)) }}
{% endmacro %}


{% macro colorField(config) %}
    {% set config = config|merge({id: config.id ?? "color#{random()}"}) %}
    {{ _self.field(config, _self.color(config)) }}
{% endmacro %}


{% macro dateTimeField(config) %}
    {% set config = config|merge({id: config.id ?? "datetime#{random()}"}) %}
    {% set input %}
        <div class="datetimewrapper">
            {{ _self.date(config) }}
            {{ _self.time(config) }}
        </div>
    {% endset %}
    {{ _self.field(config, input) }}
{% endmacro %}


{% macro textareaField(config) %}
    {% set config = config|merge({id: config.id ?? "textarea#{random()}"}) %}
    {{ _self.field(config, _self.textarea(config)) }}
{% endmacro %}


{% macro selectField(config) %}
    {% set config = config|merge({id: config.id ?? "select#{random()}"}) %}
    {{ _self.field(config, _self.select(config)) }}
{% endmacro %}


{% macro multiselectField(config) %}
    {% set config = config|merge({id: config.id ?? "multiselect#{random()}"}) %}
    {{ _self.field(config, _self.multiselect(config)) }}
{% endmacro %}


{% macro checkboxField(config) %}
    {% set config = config|merge({id: config.id ?? "checkbox#{random()}"}) %}
    {% if config.fieldLabel is defined %}
        {{ _self.field(config|merge({label: config.fieldLabel}), _self.checkbox(config)) }}
    {% else %}
        {% set instructions = config.instructions ?? null -%}
        {% set warning = config.warning ?? null -%}
        <div class="field checkboxfield{% if config.first is defined and config.first %} first{% endif %}{% if instructions %} has-instructions{% endif %}"{% if config.id is defined and config.id %} id="{{ config.id }}-field"{% endif %}>
            {{ _self.checkbox(config) }}
            {% if instructions %}
                <div class="instructions">{{ instructions|md }}</div>
            {% endif %}
            {% if warning %}
                <p class="warning with-icon">{{ warning }}</p>
            {% endif %}
        </div>
    {% endif %}
{% endmacro %}


{% macro checkboxGroupField(config) %}
    {% set config = config|merge({id: config.id ?? "checkboxgroup#{random()}"}) %}
    {{ _self.field(config, _self.checkboxGroup(config)) }}
{% endmacro %}


{% macro checkboxSelectField(config) %}
    {% set config = config|merge({id: config.id ?? "checkboxselect#{random()}"}) %}
    {{ _self.field(config, _self.checkboxSelect(config)) }}
{% endmacro %}


{% macro radioGroupField(config) %}
    {% set config = config|merge({id: config.id ?? "radiogroup#{random()}"}) %}
    {{ _self.field(config, _self.radioGroup(config)) }}
{% endmacro %}


{% macro fileField(config) %}
    {% set config = config|merge({id: config.id ?? "file#{random()}"}) %}
    {{ _self.field(config, _self.file(config)) }}
{% endmacro %}


{% macro lightswitchField(config) %}
    {% set config = config|merge({
        id: config.id ?? "lightswitch#{random()}",
        labelId: config.labelId ?? "lightswitchlabel#{random()}",
        fieldClass: (config.fieldClass ?? [])|explodeClass|push('lightswitch-field'),
    }) %}
    {{ _self.field(config, _self.lightswitch(config|withoutKey('label'))) }}
{% endmacro %}


{% macro editableTableField(config) %}
    {% set config = config|merge({id: config.id ?? "editabletable#{random()}"}) %}
    {% set input %}{% include "_includes/forms/editableTable" with config only %}{% endset %}
    {{ _self.field(config, input) }}
{% endmacro %}


{% macro elementSelectField(config) %}
    {% set config = config|merge({id: config.id ?? "elementselect#{random()}"}) %}
    {{ _self.field(config, _self.elementSelect(config)) }}
{% endmacro %}


{% macro autosuggestField(config) %}
    {% set config = config|merge({id: config.id ?? "autosuggest#{random()}"}) %}

    {# Suggest an environment variable / alias? #}
    {% if (config.suggestEnvVars ?? false) %}
        {% set value = config.value ?? '' %}
        {% if config.tip is not defined and value[0:1] not in ['$', '@'] %}
            {% set config = config|merge({
                tip: ((config.suggestAliases ?? false)
                    ? 'This can be set to an environment variable, or begin with an alias.'|t('app')
                    : 'This can be set to an environment variable.'|t('app'))
                    ~ ' <a href="https://craftcms.com/docs/3.x/config/#environmental-configuration" class="go">' ~ 'Learn more'|t('app') ~ '</a>'
            }) %}
        {% elseif config.warning is not defined and (value == '@web' or value[0:5] == '@web/') and craft.app.request.isWebAliasSetDynamically %}
            {% set config = config|merge({
                warning: 'The `@web` alias is not recommended if it is determined automatically.'|t('app')
            }) %}
        {% endif %}
    {% endif %}

    {{ _self.field(config, _self.autosuggest(config)) }}
{% endmacro %}


{% macro fieldLayoutDesignerField(config) %}
    {{ _self.field({
        label: 'Field Layout'|t('app'),
    }|merge(config), _self.fieldLayoutDesigner(config)) }}
{% endmacro %}


{# Other #}


{% macro optionShortcutLabel(key, shift, alt) %}
    {%- spaceless %}
        {% switch craft.app.request.getClientOs() %}
            {% case 'Mac' %}
                <span class="shortcut">{{ (alt ? '⌥') ~ (shift ? '⇧') ~ '⌘' ~ key }}</span>
            {% default %}
                <span class="shortcut">{{ 'Ctrl+' ~ (alt ? 'Alt+') ~ (shift ? 'Shift+') ~ key }}</span>
        {% endswitch %}
    {% endspaceless -%}
{% endmacro %}
